﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;


namespace ERPStudio
{
    public delegate void LoadDataHandler(object sender, Microsoft.Reporting.WinForms.SubreportProcessingEventArgs args);
    public delegate void RenderingComplete(object sender, Microsoft.Reporting.WinForms.RenderingCompleteEventArgs e);
    public delegate void Drillthrough(object sender, DrillthroughEventArgs e);
    public partial class PreviewRaportFrm : StudioForm
    {
        private String _NumeRaport = null;
        public String NumeRaport
        {
            get { return _NumeRaport; }
            set 
            { 
                _NumeRaport = value;
                this.reportViewer1.LocalReport.ReportPath = "Rapoarte\\" + _NumeRaport;
                
            }
        }

        private List<ReportDataSource> listaSurseDate = new List<ReportDataSource>();
        private List<ReportParameter> listaParametrii = new List<ReportParameter>();
        private List<ReportDataSource> listDataSubreport = new List<ReportDataSource>();
        public event LoadDataHandler dataloadHandler;
        public event RenderingComplete renderCompleteHandler;
        public event Drillthrough drillthroughEvent;

        void SubreportProcessingEventHandler(object sender, Microsoft.Reporting.WinForms.SubreportProcessingEventArgs e)
        {
            if (dataloadHandler != null)
                dataloadHandler(sender, e);
            /*foreach (ReportDataSource rd in listDataSubreport)
            {
                
                /*BindingSource bs = ((BindingSource)rd.Value);
                int index = 0;
                foreach (ReportParameterInfo rp in e.Parameters)
                {
                    bs.Filter = bs.Filter.Replace("@Val" + index.ToString(), rp.Values[0]);
                    index++;
                }
                e.DataSources.Add(rd);
            }*/
        }

        public PreviewRaportFrm(String numeRaport, StudioForm frm, ModuleClass mInfo)
            : base(mInfo)
        {
            InitializeComponent();
            NumeRaport = numeRaport;
            ///HandleSearch += new EventHandler(Cauta);
            HandleMoveFirst += new EventHandler(Primul);
            HandleMoveLast += new EventHandler(Ultimul);
            HandleMoveNext += new EventHandler(Urmator);
            HandleMovePrev += new EventHandler(Anterior);
            /*
            HandleAdd += new EventHandler(Adauga);
            HandleDel += new EventHandler(Sterge);
            HandleCard += new EventHandler(Card);
            HandleSalvare += new EventHandler(Salvare);
             */
            HandleTiparire += new EventHandler(PreviewRaportFrm_HandleTiparire);
            //this.reportViewer1.SetDisplayMode(DisplayMode.PrintLayout);
        }

        void PreviewRaportFrm_HandleTiparire(object sender, EventArgs e)
        {
            this.reportViewer1.PrintDialog();
        }

        private void Primul(object sender, EventArgs e)
        {
            this.reportViewer1.CurrentPage = 1;
        }
        private void Ultimul(object sender, EventArgs e)
        {
            this.reportViewer1.CurrentPage = this.reportViewer1.LocalReport.GetTotalPages();
        }
        private void Urmator(object sender, EventArgs e)
        {
            if (this.reportViewer1.CurrentPage < this.reportViewer1.LocalReport.GetTotalPages())
                this.reportViewer1.CurrentPage++;
        }
        private void Anterior(object sender, EventArgs e)
        {
            if (this.reportViewer1.CurrentPage>1)
                this.reportViewer1.CurrentPage--;
        }

        public void SetWindowFormat(Boolean landscape)
        {
            if (landscape)
            {
                this.Width = 800;
                this.Height = 600;
            }
        }
        /// <summary>
        /// adauga o sursa de date
        /// </summary>
        /// <param name="nume"> numele tabelei in raport</param>
        /// <param name="bs"> sursa de date legata</param>
        public void AddDataSource(String nume, BindingSource bs)
        {
            ReportDataSource reportDataSource = new Microsoft.Reporting.WinForms.ReportDataSource();
            reportDataSource.Name = nume;
            reportDataSource.Value = bs;
            listaSurseDate.Add(reportDataSource);
        }

        public void AddParameter(String Name, String Value, Boolean visible)
        {
            ReportParameter rp = new ReportParameter(Name, Value, visible);
            listaParametrii.Add(rp);
        }

        public void AddParameter(String Name, String[] Values, Boolean visible)
        {
            ReportParameter rp = new ReportParameter(Name, Values, visible);
            listaParametrii.Add(rp);
        }


        private void PreviewRaportFrm_Load(object sender, EventArgs e)
        {
            try
            {
                this.reportViewer1.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(SubreportProcessingEventHandler);
                reportViewer1.Drillthrough += reportViewer1_Drillthrough;
                foreach (ReportDataSource rds in listaSurseDate)
                    this.reportViewer1.LocalReport.DataSources.Add(rds);

                if (listaParametrii.Count > 0)
                    this.reportViewer1.LocalReport.SetParameters(listaParametrii);

                
                this.reportViewer1.RefreshReport();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        void reportViewer1_Drillthrough(object sender, DrillthroughEventArgs e)
        {
            if (drillthroughEvent != null)
                drillthroughEvent(sender, e);
            else
                throw new NotImplementedException();
        }

        override public void SetCurentUnit(short vCodUnitate)
        {
            this.Close();
        }

        private void reportViewer1_RenderingComplete(object sender, RenderingCompleteEventArgs e)
        {
            if (renderCompleteHandler != null)
                renderCompleteHandler(sender, e);
        }
        
    }
}
